package com.ss.android.f.b;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.applog.NetUtil;
import com.ss.android.f.DeviceRegisterManager;
import com.ss.android.f.a.RegistrationHeaderHelper;
import com.ss.android.f.a.c;

import org.json.JSONObject;
import org.json.JSONTokener;

import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

public final class RegisterServiceController {
    public static final class DeviceRegisterThread extends Thread {
        private final RegisterServiceController b;
        private int a;

        public DeviceRegisterThread(RegisterServiceController arg2) {
            super("DeviceRegisterThread");
            this.b = arg2;
            this.a = 0;
        }

        private void a() {
            Object v2_1;
            Bundle v3;
            Bundle v2;
            JSONObject v1;
            if (!f()) {
                try {
                    RegisterServiceController.a(this.b, System.currentTimeMillis());
                    if (!android.support.design.a.isNetworkAvailable(g(this.b))) {
                        return;
                    }

                    String v0_1 = RegistrationHeaderHelper.b(g(this.b));
                    if (!android.support.design.a.isTextEmpty(v0_1)) {
                        RegisterServiceController.a(this.b).put("user_agent", v0_1);
                    }

                    if (!android.support.design.a.isTextEmpty(h(this.b))) {
                        RegisterServiceController.a(this.b).put("app_track", h(this.b));
                    }

                    v1 = new JSONObject(new JSONTokener(RegisterServiceController.a(this.b).toString()));
                    v2 = new Bundle();
                    v3 = g();
                    synchronized (v3) {
                        v2.putAll(g());
                    }
                    if (v2.size() > 0) {
                        JSONObject v3_1 = new JSONObject();
                        Iterator v4 = v2.keySet().iterator();
                        while (v4.hasNext()) {
                            Object v0_3 = v4.next();
                            v3_1.put(((String) v0_3), v2.get(((String) v0_3)));
                        }

                        v1.put("custom", v3_1);
                    }

                    if (!android.support.design.a.isTextEmpty((String) null)) {
                        v1.put("google_aid", null);
                    }

                    if (!android.support.design.a.isTextEmpty((String) null)) {
                        v1.put("app_language", null);
                    }

                    if (android.support.design.a.isTextEmpty((String) null)) {
                    } else {
                        v1.put("app_region", null);
                    }
                    JSONObject v0_4 = new JSONObject();
                    v0_4.put("magic_tag", "ss_app_log");
                    v0_4.put("header", v1);
                    v0_4.put("_gen_time", System.currentTimeMillis());
                    RegisterServiceController.a(true);
                    h().set(Boolean.TRUE);
                    boolean v1_1 = this.a(v0_4.toString());
                    v2_1 = i();
                    try {
                        synchronized (v2_1) {
                            RegisterServiceController.a(false);
                            i().notifyAll();
                        }
                        try {
                            j();
                            h().remove();
                            if (v1_1) {
                                return;
                            }
                            RegisterServiceController.a(false, android.support.design.a.isTextEmpty(b.i(this.b).a((SharedPreferences) null)));
                        } catch (Exception v0) {
                            v0.printStackTrace();
                        }
                    } catch (Exception v0) {
                        v0.printStackTrace();
                    }
                } catch (Throwable v0_2) {

                }
            }
        }

        private boolean a(String str) {
            try {
                String a2;
                boolean z;
                Logger.d("RegisterServiceController", "app_log_config: " + str);
                byte[] bytes = str.getBytes("UTF-8");
                String a3 = NetUtil.a(com.ss.android.usergrowth.b.a(this.b.b, com.ss.android.f.b.aa.a()), true);
                long currentTimeMillis = System.currentTimeMillis();
                boolean z2 = currentTimeMillis - this.b.r < 600000;
                this.b.r = currentTimeMillis;
                if (Logger.debug()) {
                    Logger.d("RegisterServiceController", "request url : " + a3);
                }
                byte[] bArr = bytes.clone();
                if (com.ss.android.f.b.aa.c()) {
                    try {
                        a2 = NetUtil.a(a3, bArr, this.b.b, z2);
                    } catch (RuntimeException e) {
                        if (z2) {
                            a3 = a3 + "&config_retry=b";
                        }
                        a2 = com.bytedance.common.utility.e.a().a(a3, bytes, true, "application/json; charset=utf-8", false);
                    }
                } else {
                    if (z2) {
                        a3 = a3 + "&config_retry=b";
                    }
                    a2 = com.bytedance.common.utility.e.a().a(a3, bytes, true, "application/json; charset=utf-8", false);
                }
                if (a2 != null) {
                    if (a2.length() != 0) {
                        Logger.v("RegisterServiceController", "device_register response: " + a2);
                        JSONObject jSONObject = new JSONObject(a2);
                        this.b.p = System.currentTimeMillis();
                        this.b.m = RegistrationHeaderHelper.b();
                        SharedPreferences.Editor edit = this.b.n.edit();
                        edit.putLong("last_config_time", this.b.p);
                        edit.putInt("last_config_version", this.b.m);
                        String l = this.b.d;
                        String a4 = this.b.a.a((SharedPreferences) null);
                        boolean f = android.support.design.a.isTextEmpty(a4);
                        String optString = jSONObject.optString("install_id", null);
                        String optString2 = jSONObject.optString("device_id", null);
                        if (android.support.design.a.isTextEmpty(optString) || optString.equals(this.b.d) || optString.equalsIgnoreCase("0") || optString.equalsIgnoreCase("None")) {
                            z = false;
                        } else {
                            this.b.d = optString;
                            if (!android.support.design.a.isTextEmpty(l)) {
                                try {
                                    JSONObject jSONObject2 = new JSONObject();
                                    jSONObject2.put("old_id", l);
                                    jSONObject2.put("new_id", optString);
                                    b.a("umeng", "iid_change", jSONObject2);
                                    z = true;
                                } catch (Exception e2) {
                                    z = true;
                                }
                            }else {
                                z = true;
                            }
                        }
                        try {
                            if (!android.support.design.a.isTextEmpty(optString2) && !optString2.equals(a4) && !optString2.equalsIgnoreCase("0") && !optString2.equalsIgnoreCase("None")) {
                                if (!android.support.design.a.isTextEmpty(a4)) {
                                    try {
                                        String a5 = this.b.a.a();
                                        String e3 = this.b.a.e();
                                        String b2 = this.b.a.b();
                                        String c = this.b.a.c();
                                        String[] d = this.b.a.d();
                                        JSONObject jSONObject3 = new JSONObject();
                                        jSONObject3.put("old_id", a4);
                                        jSONObject3.put("new_id", optString2);
                                        jSONObject3.put("openudid", a5);
                                        jSONObject3.put("clientudid", b2);
                                        if (!android.support.design.a.isTextEmpty(e3)) {
                                            jSONObject3.put("udid", e3);
                                        }
                                        if (!android.support.design.a.isTextEmpty(c)) {
                                            jSONObject3.put("serial_number", c);
                                        }
                                        if (d != null && d.length > 0) {
                                            jSONObject3.put("sim_serial_number", Arrays.toString(d));
                                        }
                                        RegisterServiceController.a("umeng", "did_change", jSONObject3);
                                        z = true;
                                    } catch (Exception e4) {
                                        z = true;
                                    }
                                } else {
                                    z = true;
                                }
                            }
                            if ((android.support.design.a.isTextEmpty(optString2) || optString2.equalsIgnoreCase("0") || optString2.equalsIgnoreCase("None")) && RegisterServiceController.f != null) {
                                com.ss.android.f.a.b.a("service_monitor", "diderror", 0);
                            }
                            if (z) {
                                try {
                                    this.b.c.put("install_id", this.b.d);
                                    this.b.c.put("device_id", optString2);
                                    edit.putString("install_id", this.b.d);
                                    edit.putString("device_id", optString2);
                                } catch (Exception e5) {
                                }
                            }
                            edit.commit();
                            if (z) {
                                this.b.a.a(this.b.n);
                                m(this.b);
                            }
                            RegisterServiceController.a(true, f);
                            return true;
                        } catch (Exception e6) {
                            e6.printStackTrace();
                            return false;
                        }
                    }
                }
                return false;
            }catch (Exception e){
                e.printStackTrace();
            }
            return false;
        }

        public final void run() {
            Object v3;
            long v12 = 0;
            super.run();
            android.support.design.a.isTextEmpty(b.a(this.b).optString("device_id", null));
            b.c();
            b.b(this.b);
            while(!d()) {
                long v6 = System.currentTimeMillis();
                long v8 = 21600000;
                long v4 = 60000;
                int v0 = b.c(this.b) == RegistrationHeaderHelper.b() ? 1 : 0;
                if(!com.ss.android.f.b.aa.b() && b.e() < v12 && v0 != 0) {
                    v8 = 43200000;
                }

                if(v0 != 0) {
                    v4 = 180000;
                }

                if(this.a < 2) {
                    if(!NetUtil.a(this.b.a()) && !android.support.design.a.isTextEmpty(this.b.d)) {
                        v0 = 0;
                    }else {
                        v0 = 1;
                    }
                }
                else {
                    v0 = 0;
                }

                ++this.a;
                if(v0 != 0) {
                    v4 = 30000;
                }

                v8 = Math.max(v8 - (v6 - b.d(this.b)), v4 - (v6 - b.e(this.b)));
                if(Logger.debug()) {
                    v4 = v8 < v12 ? v6 : v6 + v8;
                    Logger.d("RegisterServiceController", "next query time : " + DateFormat.getDateTimeInstance().format(new Date(v4)));
                }

                v3 = f(this.b);
                synchronized (v3) {
                    if (v8 <= v12) {
                        if (d()) {
                            break;
                        }
                        this.a();
                    }else {
                        try {
                            if (!d()) {
                                f(this.b).wait(v8);
                                if (d()) {
                                    break;
                                }
                                this.a();
                            }
                        } catch (InterruptedException v0_1) {
                            v0_1.printStackTrace();
                        }
                    }

                    break;
                }
            }

            if(Logger.debug()) {
                Logger.d("RegisterServiceController", "DeviceRegisterThread finished");
            }

            return;
        }
    }

    public final com.ss.android.f.b.a.aa a;
    public final Context b;
    public JSONObject c;
    public String d;
    public String e;
    private static com.ss.android.f.a.b f;
    private static final Object g;
    private static c h;
    private final Object i;
    private static final Bundle j;
    private static volatile boolean k;
    private static volatile boolean l;
    private int m;
    private final SharedPreferences n;
    private static long o;
    private long p;
    private long q;
    private long r;
    private static volatile boolean s;
    private static List t;
    private static final ThreadLocal u;

    static {
        g = new Object();
        j = new Bundle();
        k = false;
        l = false;
        o = 0;
        s = false;
        t = new ArrayList();
        u = new ThreadLocal();
    }

    public RegisterServiceController(Context arg5) {

        this.i = new Object();
        this.p = 0;
        this.q = 0;
        this.r = 0;
        this.b = arg5;
        this.a = com.ss.android.f.b.a.aa.a(arg5);
        this.n = arg5.getSharedPreferences(com.ss.android.f.a.a.a(), 0);
    }

    public final String a() {
        return this.a.a(this.n);
    }

    public static void a(DeviceRegisterManager.a$a arg2) {
        if(arg2 != null) {
            t.add(new WeakReference(arg2));
        }
    }

    public static void a(com.ss.android.f.a.b arg0) {
        f = arg0;
    }

    public static void a(c arg0) {
        h = arg0;
    }

    public static void a(Context arg4) {
        if(u.get() == null && (android.support.design.a.isTextEmpty(b(arg4)))) {
            Object v2 = g;
            synchronized (v2){
                if(!k) {
                    long v0_1 = 1500;
                    if(l) {
                        v0_1 = 4000;
                    }
                    try {
                        g.wait(v0_1);
                    }catch (Exception e){

                    }
                    k = true;
                    return;
                }
            }
        }
    }

    static int a(RegisterServiceController arg0, int arg1) {
        arg0.m = arg1;
        return arg1;
    }

    static long a(RegisterServiceController arg1, long arg2) {
        arg1.q = arg2;
        return arg2;
    }

    static String a(RegisterServiceController arg0, String arg1) {
        arg0.d = arg1;
        return arg1;
    }

    static JSONObject a(RegisterServiceController arg1) {
        return arg1.c;
    }

    static void a(String arg1, String arg2, JSONObject arg3) {
        if(h != null) {
            h.a(arg1, arg2, arg3);
        }
    }

    static void a(boolean arg3, boolean arg4) {
        int v2 = t.size();
        int v1;
        for(v1 = 0; v1 < v2; ++v1) {
            Object v0 = t.get(v1);
            if(v0 != null) {
                v0 = ((WeakReference)v0).get();
                if(v0 == null) {
                   continue;
                }

                try {
                    ((DeviceRegisterManager.a$a)v0).a(arg3, arg4);
                }
                catch(Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }

    static boolean a(boolean arg0) {
        l = arg0;
        return arg0;
    }

    public static void b() {
        o = System.currentTimeMillis();
    }

    private static String b(Context arg3) {
        String v0 = null;
        try {
            v0 = com.ss.android.f.b.a.aa.a(arg3).a((SharedPreferences) null);
        } catch(IllegalArgumentException v1) {
            v1.printStackTrace();
        }

        return v0;
    }

    static long b(RegisterServiceController arg1, long arg2) {
        arg1.r = arg2;
        return arg2;
    }

    static void b(RegisterServiceController arg8) {
        long v0 = System.currentTimeMillis();
        SharedPreferences v4 = arg8.b.getSharedPreferences(com.ss.android.f.a.a.a(), 0);
        arg8.m = v4.getInt("last_config_version", 0);
        if(arg8.m == RegistrationHeaderHelper.b()) {
            long v2 = v4.getLong("last_config_time", 0);
            if(v2 <= v0) {
                v0 = v2;
            }

            arg8.p = v0;
        }

        arg8.d = v4.getString("install_id", "");
    }

    static int c(RegisterServiceController arg1) {
        return arg1.m;
    }

    static long c(RegisterServiceController arg1, long arg2) {
        arg1.p = arg2;
        return arg2;
    }

    static void c() {
        int v2 = t.size();
        int v1;
        for(v1 = 0; v1 < v2; ++v1) {
            Object v0 = t.get(v1);
            if(v0 != null) {
                ((WeakReference)v0).get();
            }
        }
    }

    static long d(RegisterServiceController arg2) {
        return arg2.p;
    }

    static boolean d() {
        return s;
    }

    static long e() {
        return o;
    }

    static long e(RegisterServiceController arg2) {
        return arg2.q;
    }

    static Object f(RegisterServiceController arg1) {
        return arg1.i;
    }

    static boolean f() {
        return l;
    }

    static Context g(RegisterServiceController arg1) {
        return arg1.b;
    }

    static Bundle g() {
        return j;
    }

    static String h(RegisterServiceController arg1) {
        return arg1.e;
    }

    static ThreadLocal h() {
        return u;
    }

    static com.ss.android.f.b.a.aa i(RegisterServiceController arg1) {
        return arg1.a;
    }

    static Object i() {
        return g;
    }

    static long j(RegisterServiceController arg2) {
        return arg2.r;
    }

    static boolean j() {
        k = true;
        return true;
    }

    static SharedPreferences k(RegisterServiceController arg1) {
        return arg1.n;
    }

    static com.ss.android.f.a.b k() {
        return f;
    }

    static String l(RegisterServiceController arg1) {
        return arg1.d;
    }

    static void m(RegisterServiceController arg5) {
        RegistrationHeaderHelper.a("install_id", arg5.d);
        RegistrationHeaderHelper.a("device_id", arg5.a.a((SharedPreferences) null));
        int v2 = t.size();
        int v1;
        for(v1 = 0; v1 < v2; ++v1) {
            Object v0 = t.get(v1);
            if(v0 != null) {
                v0 = ((WeakReference)v0).get();
                if(v0 == null) {
                    continue;
                }

                try {
                    ((DeviceRegisterManager.a$a)v0).a(arg5.a.a((SharedPreferences) null), arg5.d);
                }
                catch(Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        }
    }
}

